#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# unit-test for zxkad.help.bencode

import  sys
import  os

libpath = '..%s..%s..%slib' % (os.sep, os.sep, os.sep)
sys.path.append(libpath)

import  unittest

import  zxkad.help.bencode as bencode
import  zxkad.help.logging as logging

logger = logging.getLogger()

class   TestNode(unittest.TestCase) :
    def setUp(self) :
        logger.debug('setUp()')
        
    def tearDown(self) :
        logger.debug('tearDown()')
        
    def testEncode(self) :
        logger.debug('testEncode()')
        self.assertEqual(bencode.encode(4), 'i4e')
        self.assertEqual(bencode.encode(0), 'i0e')
        self.assertEqual(bencode.encode(-100), 'i-100e')
        self.assertEqual(bencode.encode(12345678901234567890L), 'i12345678901234567890e')
        self.assertEqual(bencode.encode(''), '0:')
        self.assertEqual(bencode.encode('abc'), '3:abc')
        self.assertEqual(bencode.encode('1234567890'), '10:1234567890')
        self.assertEqual(bencode.encode([]), 'le')
        self.assertEqual(bencode.encode([1,2,3]), 'li1ei2ei3ee')
        self.assertEqual(bencode.encode([['alice', 'bob'],[2, 3]]), 'll5:alice3:bobeli2ei3eee')
        self.assertEqual(bencode.encode({}), 'de')
        self.assertEqual(bencode.encode({'age':25, 'eye':'blue'}), 'd3:agei25e3:eye4:bluee')
        self.assertEqual(bencode.encode({'spam.mp3': {'author': 'Alice', 'length': 100000}}), 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
        
    def testDecode(self) :
        logger.debug('testEncode()')
        self.assertEqual(bencode.decode('i4e'), 4)
        self.assertEqual(bencode.decode('i0e'), 0)
        self.assertEqual(bencode.decode('i-100e'), -100)
        self.assertEqual(bencode.decode('i12345678901234567890e'), 12345678901234567890L)
        self.assertEqual(bencode.decode('0:'), '')
        self.assertEqual(bencode.decode('3:abc'), 'abc')
        self.assertEqual(bencode.decode('10:1234567890'), '1234567890')
        self.assertEqual(bencode.decode('le'), [])
        self.assertEqual(bencode.decode('li1ei2ei3ee'), [1,2,3])
        self.assertEqual(bencode.decode('ll5:alice3:bobeli2ei3eee'), [['alice', 'bob'],[2, 3]])
        self.assertEqual(bencode.decode('de'), {})
        self.assertEqual(bencode.decode('d3:agei25e3:eye4:bluee'), {'age':25, 'eye':'blue'})
        self.assertEqual(bencode.decode('d8:spam.mp3d6:author5:Alice6:lengthi100000eee'), {'spam.mp3': {'author': 'Alice', 'length': 100000}})
        
if __name__ == '__main__' :
    unittest.main()
    
